//
// Created by martin on 2/10/22.
//

#ifndef MUDUO_ENDIAN_H
#define MUDUO_ENDIAN_H

#include <stdint.h>
#include <endian.h>

namespace muduo
{
namespace net
{
namespace sockets
{

#if defined(__GNUC__)
/*
 * The inline assembler code makes type blur,
 * so we disable warnings for a while.
 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif

inline uint64_t hostToNetwork64(uint64_t host64)
{
    return htobe64(host64);
}

inline uint32_t hostToNetwork32(uint32_t host32)
{
    // = htonl
    return htobe32(host32);
}

inline uint16_t hostToNetwork16(uint16_t host16)
{
    // = htons
    return htobe16(host16);
}

inline uint64_t networkToHost64(uint64_t net64)
{
    return be64toh(net64);
}

inline uint32_t networkToHost32(uint32_t net32)
{
    // = ntohl
    return be32toh(net32);
}

inline uint16_t networkToHost16(uint16_t net16)
{
    // = ntohs
    return be16toh(net16);
}

#pragma GCC diagnostic pop


} // namespace sockets
} // namespace net
} // namespace muduo

#endif //MUDUO_ENDIAN_H
